package com.ebupt.migu.music.commons.selfdom.demo.impl.identifier;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ebupt.migu.globalpopup.enums.IdentifierEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.*;

/**
 * 枚举类型字符类型实现类
 *
 * @author wangzhu
 * @since 2021-12-31
 */
@Slf4j
@Component
public  class AbstractIdentifierEnImpl extends AbstractIdentifier {



    /**
     * 等于
     */
    private boolean equalTo(String source, String target) {
        String[] split = source.split(",|\\|");
        ArrayList arrayList = JSONObject.parseObject(JSON.toJSONString(split), ArrayList.class);
        boolean bool = arrayList.size() > 1 && target.equals("0") && arrayList.contains(target);
        return  bool?false:arrayList.contains(target);
    }

    /**
     * 不等于
     */
    private boolean noEqualTo(String source, String target) {
        String[] split = source.split(",|\\|");
        ArrayList arrayList = JSONObject.parseObject(JSON.toJSONString(split), ArrayList.class);
        boolean bool = arrayList.size() > 1 && target.equals("0")   && arrayList.contains(target);
        return  bool?true:!arrayList.contains(target);
    }

    /**
     * 仅等于
     *
     * @param source 用户身份信息
     * @param target 变色龙弹窗条件
     * @return 匹配结果
     */
    private boolean onlyEqualTo(String source, String target) {
        String[] split = source.split("/,|\\|");
        List<String> arrayList = JSON.parseArray(JSON.toJSONString(split), String.class);
        if (arrayList.size() != 1) {
            return false;
        }
        return arrayList.get(0).equals(target);
    }


    /**
     * 更具不同的判断方法返回判断结果
     */
    @Override
    public boolean getIdentifier(String source, String target, Integer identifier, Integer conditionKey) {
        boolean b = true;
        switch (identifier) {
            case 1:
                b = this.equalTo(source, target);
                break;
            case 6:
                b = this.noEqualTo(source, target);
                break;
            case 26:
                b = this.onlyEqualTo(source, target);
                break;
            default:
                break;
        }
        log.info("判断条件:{},对比参数:{},目标参数:{},对比结果:{}", IdentifierEnum.getCodeByName(identifier),source,target,b);
        return b;
    }

    @Override
    public boolean getRelation(LinkedHashMap<Boolean,Integer> map) {
        boolean t = true;
        Iterator<Map.Entry<Boolean, Integer>> iterator = map.entrySet().iterator();
        int  size=0;
        while (iterator.hasNext()) {
            Map.Entry<Boolean, Integer> next = iterator.next();
            if(size==0) {
                t = next.getKey();
            }
            //and
            if (next.getValue() == 1) {
                t = t && next.getKey();
            }
            //or
            if (next.getValue() == 2) {
                t = t || next.getKey();
            }
            size+=1;
        }
        return t;
    }

    @Override
    public <T> void sort(List<T> list) {
//        log.info("排序前集合:{},数量:{}",list, list.size() );
        list.sort((x,y)->{
            int compare = 0;
            JSONObject xparse = JSONObject.parseObject(JSON.toJSONString(x));
            JSONObject yparse =JSONObject.parseObject(JSON.toJSONString(y));
            if (xparse.get("conditionKey") != null && yparse.get("conditionKey")!=null) {
                Integer xconditionKey = Integer.valueOf(xparse.get("conditionKey").toString());
                Integer yconditionKey = Integer.valueOf(yparse.get("conditionKey").toString());
                compare = Integer.compare(xconditionKey, yconditionKey);
            }
            return compare;
        });
//        log.info("排序后集合:{},数量:{}",list, list.size() );
    }

}
